var server = require('../../../../utils/server');
Page({
  data:{
    imgs:[],
    prviewimg: [],
  },
  blur:function(event){
      var value = event.detail.value;
      this.setData({content:value});
  },
  formSubmit:function(event){

    var that = this;
    var app = getApp();
    var timeout = setTimeout(function doHandler()
       {
            var content = that.data.content;
            var user_id = app.globalData.userInfo.user_id
            var goods_id = that.data.goods_id;
            var order_id = that.data.order_id;
            var service_rank = that.data.kfValue;
            var deliver_rank = that.data.shipperValue;
            var goods_rank = that.data.descValue;
            var img = that.data.imgs;

            server.getJSON('/User/add_comment',{user_id:user_id,content:content,goods_id:goods_id,order_id:order_id,service_rank:service_rank,deliver_rank:deliver_rank,goods_rank:goods_rank,img:img},function(res){
              // success
                wx.showToast({ title: res.data.msg, icon: 'success', duration: 2000 })
                setTimeout(function doHandler(){
                  wx.navigateBack({
                    delta: 1, // 回退前 delta(默认为1) 页面
                    success: function(res){
                      // success
                    },
                    fail: function() {
                      // fail
                    },
                    complete: function() {
                      // complete
                    }
                  })
                },2000);
            });
            
			//console.log(that.data.content);
        },1000);//使用字符串执行方法


      
  },
  onLoad:function(options){
    var goods_name = options.goods_name;
    var goods_id = options.goods_id;
    var order_id = options.order_id;
    var image = options.image;
    var spec = options.spec;
    this.setData({goods_name:goods_name,image:image,spec:spec,goods_id:goods_id,order_id:order_id,starsDescIMG:"/images/stars1.gif",descValue:1,starsKFIMG:"/images/stars1.gif",kfValue:1,starsShipperIMG:"/images/stars1.gif",shipperValue:1});
    // 页面初始化 options为页面跳转所带来的参数
var that = this;
    wx.getSystemInfo({
  success: function(res) {
    that.setData({width:res.windowWidth})
  }
    })
  


  },

  //添加Banner  
  bindChooiceProduct: function () {
    var that = this;
    var imgs = that.data.imgs;
    var prviewimg = that.data.prviewimg;
    wx.chooseImage({
      count: 3,  //最多可以选择的图片总数  
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
      success: function (res) {
        console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
        var tempFilePaths = res.tempFilePaths;
        if (tempFilePaths.length >3){
          wx.showToast({
            title: '最多上传三张',
            icon:'none'
          })
          return;
        }
        console.log(res)
        //启动上传等待中...  
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 10000
        })
        var uploadImgCount = 0;
        for (var i = 0, h = tempFilePaths.length; i < h; i++) {
          wx.uploadFile({
            url: "https://www.donglongshangcheng.com" + '/Admin/Ueditor/imageUp/savepath/comment/pictitle/banner/dir/images',
            filePath: tempFilePaths[i],
            name: 'file',
            formData: {
              'imgIndex': "comment"
            },
            header: {
              "Content-Type": "multipart/form-data"
            },
            success: function (res) {
              console.log(res)
              var data = JSON.parse(res.data);
              imgs.push(data.url);
              prviewimg[uploadImgCount] = "https://www.donglongshangcheng.com" + data.url;
              uploadImgCount++;
              
              //服务器返回格式: { "Catalog": "testFolder", "FileName": "1.jpg", "Url": "https://test.com/1.jpg" }  
              // var productInfo = that.data.productInfo;
              // if (productInfo.bannerInfo == null) {
              //   productInfo.bannerInfo = [];
              // }
              // productInfo.bannerInfo.push({
              //   "catalog": data.Catalog,
              //   "fileName": data.FileName,
              //   "url": data.Url
              // });
              that.setData({
                imgs: imgs,
                prviewimg: prviewimg,
              });

              //如果是最后一张,则隐藏等待中  
              if (uploadImgCount == tempFilePaths.length) {
                wx.hideToast();
              }
            },
            fail: function (res) {
              wx.hideToast();
              wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false,
                success: function (res) { }
              })
            }
          });
        }
      }
    });
  } ,
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  starsDesc:function(e){
     var target = e.currentTarget.dataset;
     var width = this.data.width;
     var sWidth = width / 750.0 * 225.0;
     var offsetLeft = e.detail.x - e.currentTarget.offsetLeft;
     var clickPos = 0;
     var starsDesc = "/images/stars1.gif";
     if(sWidth / 5.0 > offsetLeft)
     {
     clickPos = 0;
     starsDesc = "/images/stars1.gif";
     }
     else if(sWidth / 5.0 * 2 > offsetLeft)
     {
       clickPos = 1;
       starsDesc = "/images/stars2.gif";
     }
     else if(sWidth / 5.0 * 3 > offsetLeft)
     {clickPos = 2;
     starsDesc = "/images/stars3.gif";
     }
     else if(sWidth / 5.0 * 4 > offsetLeft)
     {clickPos = 3;
     starsDesc = "/images/stars4.gif";}
     else if(sWidth / 5.0 * 5 > offsetLeft)
     {clickPos = 4;
     starsDesc = "/images/stars5.gif";}
     this.setData({descValue:++clickPos,starsDescIMG:starsDesc});
  },
  starsKF:function(e){
     var target = e.currentTarget.dataset;
     var width = this.data.width;
     var sWidth = width / 750.0 * 225.0;
     var offsetLeft = e.detail.x - e.currentTarget.offsetLeft;
     var clickPos = 0;
     var starsDesc = "/images/stars1.gif";
     if(sWidth / 5.0 > offsetLeft)
     {
     clickPos = 0;
     starsDesc = "/images/stars1.gif";
     }
     else if(sWidth / 5.0 * 2 > offsetLeft)
     {
       clickPos = 1;
       starsDesc = "/images/stars2.gif";
     }
     else if(sWidth / 5.0 * 3 > offsetLeft)
     {clickPos = 2;
     starsDesc = "/images/stars3.gif";
     }
     else if(sWidth / 5.0 * 4 > offsetLeft)
     {clickPos = 3;
     starsDesc = "/images/stars4.gif";}
     else if(sWidth / 5.0 * 5 > offsetLeft)
     {clickPos = 4;
     starsDesc = "/images/stars5.gif";}
     this.setData({kfValue:++clickPos,starsKFIMG:starsDesc});
  },
  starsShipper:function(e){
     var target = e.currentTarget.dataset;
     var width = this.data.width;
     var sWidth = width / 750.0 * 225.0;
     var offsetLeft = e.detail.x - e.currentTarget.offsetLeft;
     var clickPos = 0;
     var starsDesc = "/images/stars1.gif";
     if(sWidth / 5.0 > offsetLeft)
     {
     clickPos = 0;
     starsDesc = "/images/stars1.gif";
     }
     else if(sWidth / 5.0 * 2 > offsetLeft)
     {
       clickPos = 1;
       starsDesc = "/images/stars2.gif";
     }
     else if(sWidth / 5.0 * 3 > offsetLeft)
     {clickPos = 2;
     starsDesc = "/images/stars3.gif";
     }
     else if(sWidth / 5.0 * 4 > offsetLeft)
     {clickPos = 3;
     starsDesc = "/images/stars4.gif";}
     else if(sWidth / 5.0 * 5 > offsetLeft)
     {clickPos = 4;
     starsDesc = "/images/stars5.gif";}
     this.setData({shipperValue:++clickPos,starsShipperIMG:starsDesc});
  }
})